package com.jspgou.cms.action.member;

import static com.jspgou.cms.Constants.MEMBER_SYS;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jspgou.cms.entity.Aftersale;
import com.jspgou.cms.entity.AftersaleDemand;
import com.jspgou.cms.entity.AftersaleType;
import com.jspgou.cms.entity.Order;
import com.jspgou.cms.entity.ShopMember;
import com.jspgou.cms.manager.AftersaleDemandMng;
import com.jspgou.cms.manager.AftersaleMng;
import com.jspgou.cms.manager.AftersaleTypeMng;
import com.jspgou.cms.manager.OrderMng;
import com.jspgou.cms.web.CmsUtils;
import com.jspgou.cms.web.ShopFrontHelper;
import com.jspgou.cms.web.SiteUtils;
import com.jspgou.common.web.ResponseUtils;
import com.jspgou.common.web.springmvc.MessageResolver;
import com.jspgou.core.entity.Website;

@Controller
public class AftersaleFroAct {
	
	/**
	 * 会员售后添加页
	 */
	private static final String TPL_AFTERSALE_ADD="tpl.aftersaleAdd";
	/**
	 * 会员售后列表页
	 */
	private static final String TPL_AFTERSALE_LIST="tpl.aftersaleList";
	/**
	 * 会员售后详情页
	 */
	private static final String TPL_AFTERSALE_VIEW="tpl.aftersaleView";
	/**
	 * 会员售后详情页
	 */
	private static final String TPL_AFTERSALE_EDIT="tpl.aftersaleEdit";
	
	// 会员咨询管理
	@RequestMapping(value = "/aftersale/list*.jspx")
	public String list(String cl,Integer pageNo,HttpServletRequest request,
			ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		model.addAttribute("cl", cl);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,TPL_AFTERSALE_LIST), request);
	}
	
	// 会员售后添加
	@RequestMapping(value = "/aftersale/view.jspx")
	public String view(Long id,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Aftersale aftersale=manager.findById(id);
		model.addAttribute("aftersale", aftersale);
		Order order=aftersale.getOrder();
		model.addAttribute("order", order);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,TPL_AFTERSALE_VIEW), request);
	}
	
	// 会员售后添加
	@RequestMapping(value = "/aftersale/add.jspx")
	public String add(Long orderId,Boolean deliver,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Order order=orderMng.findById(orderId);
		List<AftersaleType> tlist=aftersaleTypeMng.findByDeliver(deliver);
		model.addAttribute("order", order);
		model.addAttribute("tlist", tlist);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,TPL_AFTERSALE_ADD), request);
		
	}
	
	// 会员售后保存
	@RequestMapping(value = "/aftersale/save.jspx", method = RequestMethod.POST)
	public String save(Aftersale bean,Long typeId,Long demandId,Long orderId,
			String[] fashionPic,HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		manager.save(bean,web,typeId,demandId,orderId,member.getId(),fashionPic);
		Order order=orderMng.findById(orderId);
        order.setAfter(true);
        orderMng.updateByUpdater(order);
		model.addAttribute("order", order);
		return "redirect:list.jspx";
	}
	
	// 会员售后修改
	@RequestMapping(value = "/aftersale/edit.jspx")
	public String edit(Long id,
			HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		Aftersale aftersale=manager.findById(id);
		List<AftersaleType> tlist=aftersaleTypeMng.findAll();
		List<AftersaleDemand> dlist=new ArrayList<AftersaleDemand>(aftersale.getType().getDemands());
		Order order=orderMng.findById(aftersale.getOrder().getId());
		model.addAttribute("tlist", tlist);
		model.addAttribute("dlist", dlist);
		model.addAttribute("aftersale", aftersale);
		model.addAttribute("order", order);
		ShopFrontHelper.setCommonData(request, model, web);
		ShopFrontHelper.frontPageData(request, model);
		return web.getTplSys(MEMBER_SYS, MessageResolver.getMessage(request,TPL_AFTERSALE_EDIT), request);
	}
	
	// 会员售后保存
	@RequestMapping(value = "/aftersale/update.jspx", method = RequestMethod.POST)
	public String update(Aftersale bean,Long orderId,Long typeId,Long demandId,
			String[] fashionPic,HttpServletRequest request,ModelMap model) {
		Website web = SiteUtils.getWeb(request);
		ShopMember member=CmsUtils.getMember(request);
		//用户没有登录，跳转到登录页
		if(member == null||member.getIsEmployee()){
			return ShopFrontHelper.showLogin(request, model, web);
		}
		manager.update(bean,web,typeId,demandId,orderId,member.getId(),fashionPic);
		Order order=orderMng.findById(orderId);
		model.addAttribute("order", order);
		return "redirect:list.jspx";
	}
	
	
	// 会员售后服务要求
	@RequestMapping(value = "/aftersale/demandlist.jspx")
	public void demandList(Long typeId,HttpServletRequest request,
			HttpServletResponse response, ModelMap model)throws JSONException {
		AftersaleType aftersaleType=aftersaleTypeMng.findById(typeId);
		List<AftersaleDemand> list=new ArrayList<AftersaleDemand>(aftersaleType.getDemands());
		Long ids[]=new Long[list.size()];
		String names[]=new String[list.size()];
		for(int i=0;i<list.size();i++){
			ids[i]=list.get(i).getId();
			names[i]=list.get(i).getName();
		}
		JSONObject json = new JSONObject();
		try {
			json.put("success", true);
			json.put("ids", ids);
			json.put("names", names);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		ResponseUtils.renderJson(response, json.toString());
	}
	
	// 会员售后服务是否需要退款
	@RequestMapping(value = "/aftersale/demandRefundMoney.jspx")
	public void demandRefundMoney(Long demandId,HttpServletRequest request,
			HttpServletResponse response, ModelMap model)throws JSONException {
		AftersaleDemand bean=aftersaleDemandMng.findById(demandId);
		JSONObject json = new JSONObject();
		try {
			json.put("success", bean.getIsRefund());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		ResponseUtils.renderJson(response, json.toString());
	}
	
	@Autowired
	private AftersaleMng manager;
	@Autowired
	private AftersaleTypeMng aftersaleTypeMng;
	@Autowired
	private OrderMng orderMng;
	@Autowired
	private AftersaleDemandMng aftersaleDemandMng;
}
